package action;

import javax.servlet.http.HttpSession;

import model.LogInfo;
import model.PageBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import util.ResponseUtil;
import dao.LogInfoDao;

public class LogInfoAction extends BaseAction {

	private static final long serialVersionUID = 4740052267899530914L;
	
	private String page;
	private String rows;
	
	private String owner;
	private String groupName;
	
	private LogInfo logInfo;
	
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	
	public LogInfo getLogInfo() {
		return logInfo;
	}

	public void setLogInfo(LogInfo logInfo) {
		this.logInfo = logInfo;
	}

	LogInfoDao logInfoDao = new LogInfoDao();

	@Override
	public String execute() throws Exception {
		PageBean pageBean = new PageBean(page == null? 0 : Integer.parseInt(page),
				rows == null ? 0 : Integer.parseInt(rows));
		HttpSession session = request.getSession();
		JSONObject result = new JSONObject();
		try {
			JSONArray jsonArray = logInfoDao.getLogInfoByOwnerGroup(owner, groupName, pageBean);
			int total = logInfoDao.getLogInfoCountByOwnerGroup(owner, groupName);
			result.put("rows", jsonArray);
			result.put("total", total);
			result.put("success", "true");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

	public String delete() throws Exception {
		JSONObject result = new JSONObject();
		try {
			int delNums = logInfoDao.deleteLogInfo(owner, groupName);
			if (delNums > 0) {
				result.put("success", "true");
				result.put("delNums", delNums);
			} else {
				result.put("success", "false");
				result.put("errorMsg", "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

	public String save() throws Exception {
		JSONObject result = new JSONObject();
		try {
			int saveNums = logInfoDao.addLogInfo(logInfo);
			if (saveNums > 0) {
				result.put("success", "true");
			} else {
				result.put("success", "false");
				result.put("errorMsg", "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

}
